import Vue from 'vue'

import VueRouter from 'vue-router'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/login', component: () => import('@/components/Login.vue') },
    { 
      path: '/', 
      redirect: '/user',
      component: () => import('@/components/Home.vue'),
      children: [
        { path: '/user', component: () => import('@/components/menus/User.vue') },
        { path: '/right', component: () => import('@/components/menus/Right.vue') },
        { path: '/good', component: () => import('@/components/menus/Good.vue') },
        { path: '/order', component: () => import('@/components/menus/Order.vue') },
        { path: '/setting', component: () => import('@/components/menus/Setting.vue') },
        { path: '/userDetail/:id', component: () => import('@/components/user/UserDetail.vue')},
      ]
    },


    // { 
    //   path: '/', 
    //   redirect: '/home',
    // },
    // { 
    //   path: '/home', 
    //   // redirect: '/home',
    //   component: () => import('@/components/Home.vue'),
    //   children: [
    //     { path: 'user', component: () => import('@/components/menus/User.vue') },
    //     { path: 'right', component: () => import('@/components/menus/Right.vue') },
    //     { path: 'good', component: () => import('@/components/menus/Good.vue') },
    //     { path: 'order', component: () => import('@/components/menus/Order.vue') },
    //     { path: 'setting', component: () => import('@/components/menus/Setting.vue') },
    //     { path: 'userDetail/:id', component: () => import('@/components/user/UserDetail.vue') }
    //   ]
    // }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router